package com.cola.colaboot.module.system.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.cola.colaboot.config.dto.Res;
import com.cola.colaboot.config.exception.ColaException;
import com.cola.colaboot.config.security.SecurityUtils;
import com.cola.colaboot.module.system.pojo.SysFile;
import com.cola.colaboot.module.system.pojo.SysUser;
import com.cola.colaboot.module.system.service.SysFileService;
import com.cola.colaboot.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/file")
public class SysFileController {
    @Autowired
    private SysFileService sysFileService;

    public static final String FILE_ROOT_URL = "/root/upload/cola/";

    /**
     *  文件上传
     * @param dir   文件夹名
     * @param oldId 用于更新文件
     */
    @PostMapping("/upload")
    public Res<?> upload(@RequestParam("file") MultipartFile multipartFile,
             @RequestParam("dir") String dir, @RequestParam(value = "oldId",required = false) String oldId){
        SysUser sysUser = SecurityUtils.currentUser();
        if(StringUtils.isBlank(dir)){
            throw new ColaException("文件夹名不能为空");
        }
        if (multipartFile.getSize() > 10 * 1024 * 1024){
            throw new ColaException("文件过大");
        }
        String fileName = multipartFile.getOriginalFilename();
        File file;
        SysFile sysFile;
        if (StringUtils.isNotBlank(oldId) && !"null".equals(oldId)){//更新老文件
            sysFile = sysFileService.getById(oldId);
            String oldFileUrl = FILE_ROOT_URL + sysFile.getFileUrl();
            String oldFileDir = oldFileUrl.substring(0, oldFileUrl.lastIndexOf("/")+1);
            String oldFileName = oldFileUrl.substring(oldFileUrl.lastIndexOf("/")+1);
            file = FileUtil.saveFile(oldFileDir, oldFileName, multipartFile);
        }else{
            file = FileUtil.saveFile(FILE_ROOT_URL + dir, "", multipartFile);
            sysFile = new SysFile();
        }
        sysFile.setFileName(fileName);
        sysFile.setSuffix(fileName.substring(fileName.lastIndexOf(".")));
        sysFile.setFileSize(multipartFile.getSize());
        sysFile.setFileUrl(dir+"/"+file.getName());
        sysFile.setCreateUser(sysUser.getId());
        sysFileService.saveOrUpdate(sysFile);
        return Res.ok("success",sysFile.getId());
    }

    @GetMapping("/img/{id}")
    public void download(@PathVariable String id, HttpServletResponse response) throws IOException {
        SysFile sysFile = sysFileService.getById(id);
        if (sysFile == null) throw new ColaException("文件查找失败");
        File file = new File(FILE_ROOT_URL+sysFile.getFileUrl());
        FileUtil.download(file, "image/jpeg", true, response);
    }

    @GetMapping("/download")
    public void download(String id, String contentType, HttpServletResponse response) throws IOException {
        if (StringUtils.isBlank(contentType)){
            contentType = "application/x-download";
        }
        SysFile sysFile = sysFileService.getById(id);
        File file = new File(FILE_ROOT_URL+sysFile.getFileUrl());
        FileUtil.download(file, contentType,false, response);
    }
}
